﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OfficialWeb.Application;

using OOS.Application.Setmeal.Services.Interfaces;
using OOS.Application.User.Services.Interfaces;
using OOS.Core.Setmeal;

namespace OOS.Application.Setmeal.Services
{
    /// <summary>
    /// 套餐菜品关系管理
    /// </summary>
    public class SetmealDishService : ISetmealDishService, ITransient
    {
        private readonly ISqlSugarClient _client;

        public SetmealDishService(ISqlSugarClient client)
        {
            _client = client;
        }

        /// <summary>
        /// 根据套餐id删除套餐菜品关系
        /// </summary>
        /// <param name="setmealId"></param>
        /// <returns></returns>
        public bool DeleteSetmealDishBysetmealId(int setmealId)
        {
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            List<SetmealDish> setmealDishes = SelectSetmealDishBysetmealId(setmealId);
            if (setmealDishes == null || setmealDishes.Count <= 0) return true;
            for (int i = 0; i < setmealDishes.Count;i++)
            {
                setmealDishes[i].UpdateUser = userId;
                setmealDishes[i].UpdateDate = DateTime.Now;
                setmealDishes[i].IsDeleted = true;
            }
            return _client.Updateable(setmealDishes).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 新增套餐菜品关系
        /// </summary>
        /// <param name="setmealDish"></param>
        /// <param name="setmealId"></param>
        /// <returns></returns>
        public bool InsertSetmealDish(SetmealDish setmealDish, int setmealId)
        {
            if(setmealDish == null) { return false; }
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            setmealDish.SetmealId = setmealId;
            setmealDish.CreateUser = userId;
            setmealDish.CreateDate = DateTime.Now;
            setmealDish.UpdateUser = userId;
            setmealDish.UpdateDate = DateTime.Now;
            setmealDish.IsDeleted = false;
            return _client.Insertable(setmealDish).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据套餐id查询所有套餐菜品关系
        /// </summary>
        /// <param name="setmealId"></param>
        /// <returns></returns>
        public List<SetmealDish> SelectSetmealDishBysetmealId(int setmealId)
        {
            return _client.Queryable<SetmealDish>()
                .Where(sd => sd.IsDeleted == false)
                .Where(sd => sd.SetmealId == setmealId)
                .ToList();
        }
    }
}
